"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DMS分布式锁
Case Name   : 同一session对不同对象获取X锁
Create At   : 2023/4/24
Owner       : opentestcase033
Description :
    1.创建测试表
    2.在主节点开启事务alter不同表
    3.清理环境
Expect:
    1.成功
    2.执行成功，获取锁
    3.成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class DmsOck(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.table1 = 't_dms_ock_0034_1'
        self.table2 = 't_dms_ock_0034_2'

    def test_dms_ock(self):
        text1 = '-----step1:创建测试表;expect:成功-----'
        self.log.info(text1)
        create_tb = \
            f"drop table if exists {self.table1},{self.table2} cascade;" \
            f"drop table if exists  cascade;" \
            f"create table {self.table1}(" \
            f"id int, name varchar, source number) with(segment=on);" \
            f"create table {self.table2}(" \
            f"id int, name varchar, source number) with(segment=on);" \
            f"insert into {self.table1} values(1,'haha',92),(2,'tutu',85);" \
            f"insert into {self.table2} values(5,'asd',50),(7,'zxc',30);"
        create_tb_res = self.pri_sh.execut_db_sql(create_tb)
        self.log.info(create_tb_res)
        self.assertTrue(self.constant.CREATE_TABLE_SUCCESS in create_tb_res and
                        self.constant.INSERT_SUCCESS_MSG in create_tb_res,
                        f"执行失败:{text1}")

        text2 = '-----step2:在主节点开启事务alter不同表;expect:执行成功，获取锁-----'
        self.log.info(text2)
        sql = f"start transaction;" \
              f"alter table {self.table1} rename to new_{self.table1};" \
              f"alter table {self.table2} add class varchar(10);" \
              f"select locktype, relname, mode from pg_locks, pg_class " \
              f"where relation = oid " \
              f"and relname in ({self.table1}, {self.table2});"
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertIn(self.constant.START_TRANSACTION_SUCCESS_MSG,
                      sql_res, f"执行失败:{text2}")

    def tearDown(self):
        text3 = '-----step3:清理环境 expect:成功-----'
        self.log.info(text3)
        drop_tb_res = self.pri_sh.execut_db_sql(
            f"drop table if exists {self.table1};"
            f"drop table if exists new_{self.table1};"
            f"drop table if exists {self.table2};")
        self.log.info(drop_tb_res)
        self.assertEqual(drop_tb_res.count(self.constant.DROP_TABLE_SUCCESS),
                         3, f"删表失败:{text3}")
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
